package myzd.mapper;

import myzd.domain.UserRoles;
import myzd.domain.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zks
 * @since 2017/10/26 9:14
 */
@Mapper
public interface UserMapper {

  @Select("SELECT * FROM users WHERE email=#{email};")
  Users getUserByEmail(@Param("email") String email);

  @Select("SELECT r.`id` AS 'id', r.`name` AS 'name', r.`slug` AS 'slug' FROM role_user u LEFT JOIN roles r ON u.`role_id`=r.`id` WHERE u.`user_id`=#{userId};")
  List<UserRoles> getUserRoles(@Param("userId") Long userId);

  @Insert("INSERT INTO users(mobile, NAME, email, PASSWORD, remember_token, created_at, updated_at) VALUE(" +
    "#{users.mobile}," +
    "#{users.name}," +
    "#{users.email}," +
    "#{users.password}," +
    "#{users.rememberToken}," +
    "#{users.createdAt}," +
    "#{users.updatedAt}" +
    ");")
  @Options(useGeneratedKeys = true, keyProperty = "users.id")
  void createUsers(@Param("users") Users users);

  @Select("INSERT INTO role_user(role_id,user_id,created_at,updated_at) value(" +
    "#{roleId},#{userId},NOW(),NOW()" +
    ");")
  Integer createUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
}
